package com.cheerscpl.network;

import com.cheerscpl.protocol.DefaultParser;
import com.cheerscpl.protocol.model.DataPacket;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by chenpuliang on 2017/6/3.
 */
public class Decoder extends SimpleChannelInboundHandler<String> {

    private static final Logger logger = LoggerFactory.getLogger(Decoder.class);

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception {
        if(msg != null){
            DataPacket dataPacket = DefaultParser.parse(msg);
            if(dataPacket != null){
                ctx.fireChannelRead(dataPacket);
                logger.info("Received Message of Device: {}, is: {}", dataPacket.getDevId(), msg);
            }
        }
    }
}
